//
//  ViewController.m
//  DemoProject
//
//  Created by zhl on 2017/2/8.
//  Copyright © 2017年 zhl. All rights reserved.
//
#import "ViewController.h"
#import<JZMergeSDK/JZMergeSDK.h>
#import "MD5.h"
#import "PayViewController.h"
#import "MBProgressHUD.h"
#import <Photos/Photos.h>
#import "HLMBProgressHUD.h"
//static NSString *KEYSTR     = @"e528b97251e4948d45703219eb6721e7";
//static NSString *IVSTR      = @"916574485ce5983d";
//static NSString *USERAPPID  = @"20160311174349049270";
//static NSString *KEYSTR     = @"dd9e589f9fd4eb23de09d69a6f93805d";
//static NSString *IVSTR      = @"5222bb6c9c7ba990";
//static NSString *USERAPPID  = @"20170524160327804067";
@interface ViewController ()<JZMSDKPayDelegate>{
    NSString *CompKey;
}
@property (weak, nonatomic) IBOutlet UITextField *priceText;
@property (nullable,strong,nonatomic)  JZMTokenModel *tokenModel;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)pay:(UIButton*)sender {
    if (!self.priceText.text) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    });
     _tokenModel = [JZMTokenModel new];
    if ([self.serviceType isEqualToString:@"JFT"]) {
//        _tokenModel.p1_user_code =@"10208331";
//        _tokenModel.userAppid = @"20161111102907658374";
//        _tokenModel.keyString = @"1b4f2dd8a284638cf325122bcc69af31";
//        _tokenModel.ivString  = @"2995d86414449a62";
//        CompKey              = @"40F4A509B7459D667B68C9A51A4E14DA";
        _tokenModel.p1_user_code =@"10200789";
        _tokenModel.userAppid = @"20170524160327804067";
        _tokenModel.keyString = @"dd9e589f9fd4eb23de09d69a6f93805d";
        _tokenModel.ivString  = @"5222bb6c9c7ba990";
        CompKey              = @"D1C8CAB76BFEFA0C713B60CAAABA1A9B";


    }
    if ([self.serviceType isEqualToString:@"YHB"]) {
        _tokenModel.p1_user_code =@"3010200789";
        _tokenModel.userAppid = @"30160113111756317882";
        _tokenModel.keyString = @"5671a82525ad10e66bcd738356d4da11";
        _tokenModel.ivString  = @"6181b7a9075b7280";
        CompKey              = @"D1C8CAB76BFEFA0C713B60CAAABA1A9B";
    }
    if ([self.serviceType isEqualToString:@"YZF"]) {
        _tokenModel.p1_user_code =@"4010200789";
        _tokenModel.userAppid = @"40160526105701396297";
        _tokenModel.keyString = @"f3ea6060635f305e3d9613b919031bbb";
        _tokenModel.ivString  = @"32c586fafbc77362";
        CompKey              = @"D1C8CAB76BFEFA0C713B60CAAABA1A9B";    }
    if ([self.serviceType isEqualToString:@"WSF"]) {
        _tokenModel.p1_user_code =@"5010200789";
        _tokenModel.userAppid = @"50171019094042507146";
        _tokenModel.keyString = @"93092240cb6ed32f0129baeab0ef417f";
        _tokenModel.ivString  = @"1841b67782265ae1";
        CompKey              = @"1153CF5C7E831DC90487366C960B20E3";
//        tokenModel.p1_user_code =@"5010202775";
//        tokenModel.userAppid = @"50161117171557787094";
//        tokenModel.keyString = @"c0f4809047560bbe3ea54c9adb7662b5";
//        tokenModel.ivString  = @"512c8049e9c312ec";
//        CompKey              = @"BFBBE5797C4AFB59C0750DF42ADF34A7";
    }
    if ([self.serviceType isEqualToString:@"QAO"]) {
        _tokenModel.p1_user_code =@"6010203402";
        _tokenModel.userAppid = @"60170316104034711742";
        _tokenModel.keyString = @"e041f17854b3ba4b04d4921377f3e4b3";
        _tokenModel.ivString  = @"48592ff28aefd095";
        CompKey              = @"CD4EFE5B29403EDD3441A88963D8F7B0";
    }
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate *p2_orderDate=[NSDate date];
    _tokenModel.p2_order=[formatter stringFromDate:p2_orderDate];
    _tokenModel.p3_money = self.priceText.text;
    _tokenModel.p4_returnurl = @"http://shangjia.jtpay.com/Form/TestReturn";
    _tokenModel.p5_notifyurl = @"http://shangjia.jtpay.com/Form/TestNotice";;
    _tokenModel.p6_ordertime = [formatter stringFromDate:p2_orderDate];
    NSString *p7_sign=[NSString stringWithFormat:@"%@&%@&%@&%@&%@&%@%@",_tokenModel.p1_user_code,_tokenModel.p2_order,_tokenModel.p3_money,_tokenModel.p4_returnurl,_tokenModel.p5_notifyurl,_tokenModel.p6_ordertime,CompKey];
    _tokenModel.p7_sign = [MD5 md532BitUpper:p7_sign];
    _tokenModel.serviceType = self.serviceType;
    //以下为可空参数
    _tokenModel.parameterDic =@{
                               @"p8_signtype":@"",@"p10_paychannelnum":@"",@"p11_cardtype":@"",@"p12_channel":@"",@"p13_orderfailertime":@"",@"p14_customname":@"",@"p15_customcontacttype":@"",@"p16_customcontact":@"",@"p17_customip":@"",@"p18_product":@"",@"p19_productcat":@"",@"p20_productnum":@"",@"p21_pdesc":@"",@"p22_version":@"",@"p23_charset":@"",@"p24_remark":@"",@"P27_phonecharacter":@""};
    
    if (sender.tag==3) {
        _tokenModel.payType =@"3";
        _tokenModel.controler =self;
        _tokenModel.isReturn =YES;
          [JZMPay getToken:_tokenModel returnDelegate:self];
    }else if(sender.tag==4){
        _tokenModel.controler =self;
        _tokenModel.isReturn =YES;
        _tokenModel.payType =@"4";
        [JZMPay getToken:_tokenModel returnDelegate:self];
    }else if(sender.tag==11){
        _tokenModel.controler =self;
        _tokenModel.isReturn =YES;
        _tokenModel.payType =@"11";
        [JZMPay getToken:_tokenModel returnDelegate:self];
    }else if(sender.tag==12){
        _tokenModel.controler =self;
        _tokenModel.isReturn =YES;
        _tokenModel.payType =@"12";
        [JZMPay getToken:_tokenModel returnDelegate:self];
    }else{
        //http://39.106.161.218:8001
        //http://39.107.59.151:8001
  [JZMPay getPayTypeListWithAppId:_tokenModel.userAppid userCode:_tokenModel.p1_user_code key:_tokenModel.keyString iv:_tokenModel.ivString serviceType:_tokenModel.serviceType zzApi:@"http://39.107.59.151:8001" delegate:self];
    }
}

- (IBAction)clernButton:(UIButton *)sender {
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"JZMMIWENKEY"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"NONCERKEY"];
}

- (void)getPayTypeListSuccess:(NSArray *)list{
    __weak typeof(self) weakSelf =self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      __block PayViewController *payVC =[PayViewController new];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            payVC.parArray = list;
            payVC.tokenModel = _tokenModel;
            payVC.cmkey    = CompKey;
            [weakSelf.navigationController pushViewController:payVC animated:YES];
        });
    });
}

- (void)getPayTypeListFailure:(NSString *)message{
    NSLog(@"%@",message);
}

- (void)openAppSuccessed{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
}

- (void)openAppFailer:(NSString *)failer{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([MBProgressHUD hideHUDForView:self.view animated:YES]) {
                [HLMBProgressHUD addMBProgressHUDInView:self.view hudMode:5 hideDelay:1.2 hudDetailText:failer];
        }
    });
}

- (void)jftPayResult:(NSString *)result{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [HLMBProgressHUD addMBProgressHUDInView:self.view hudMode:5 hideDelay:1.2 hudDetailText:result];
    });
}

- (void)jftPayFailure:(NSString *)message{
    dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
 [HLMBProgressHUD addMBProgressHUDInView:self.view hudMode:5 hideDelay:1.2 hudDetailText:message];
    });
}

- (void)jftPaySuccess{
}
@end
